package com.lxb.store.controller;

import com.lxb.store.entity.User;
import com.lxb.store.service.UserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.HashMap;
import java.util.Map;

/**
 * 修改密码
 */
@Controller
public class ModifiyController {

    @Autowired
    private UserService userService;

    @GetMapping("/modify")
    public String modifyIndex(){

        return "modify";
    }

    @PostMapping("/modify")
    @ResponseBody
    public Map<String,String> modify(User user){
        Map<String,String> ret = new HashMap<>();

        User existUser = userService.getByUsername(user.getUsername().trim());

        //判断用户名是否存在
        if(existUser == null){
            ret.put("msg","用户名不存在");
            ret.put("status","error");
            return  ret;
        }
        //判断邮箱是否正确
        if(!existUser.getEmail().equals(user.getEmail().trim())){
            ret.put("msg","邮箱错误");
            ret.put("status","error");
            return  ret;
        }

        //修改密码
        String password = user.getPassword().trim();
        existUser.setPassword(password);
        User updateUser = userService.update(existUser);
        if(updateUser == null){
            ret.put("msg","修改密码错误");
            ret.put("status","error");
            return  ret;
        }

        ret.put("msg","修改密码成功");
        ret.put("status","success");
        return  ret;
    }

}
